package com.service.impl;

import com.dao.FilmMapper;
import com.dao.PlayMapper;
import com.dao.RoomMapper;
import com.entity.Film;
import com.entity.Play;
import com.entity.Room;
import com.service.PlayService;
import com.vo.PlayDetailVo;
import com.vo.PlayJson;
import com.vo.PlayVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PlayServiceImpl implements PlayService {

    @Autowired
    private PlayMapper playMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private FilmMapper filmMapper;
    @Override
    public List<PlayVo> findPalyById(String filmId) {
        List<Play> plays =playMapper.selectByFilmId(filmId);
        List<PlayVo> playVos = new ArrayList<>();
        for (Play play : plays) {
            PlayVo vo = new PlayVo();
            vo.setpId(play.getpId());
            vo.setpTime(play.getpTime());
            vo.setLanType(play.getLanType());
            vo.setPrice(play.getPrice());
            vo.setRoomId(play.getRoomId());
            vo.setRoomName(play.getRoomName());
            vo.setfId(play.getfId());
            vo.setfName(play.getfName());
            playVos.add(vo);
        }
        return playVos;
    }

    @Override
    public PlayDetailVo getDetailById(String playId) {
        Play play = playMapper.selectByPlayId(playId);
        PlayDetailVo detailVo = new PlayDetailVo();
        // 对属性名相同且类型相同的对象  进行拷贝
        // 第一个参数是source  源对象   第二个参数是target  目标对象
        // 拷贝其实拷贝的是两者的交集  将所有同名属性赋值进来
        // 底层使用的是反射   调用get和set方法
        BeanUtils.copyProperties(play, detailVo);

        // 对于类型不同的属性值  应该设置不同的属性名  然后单独处理
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
        StringBuffer dateStr = new StringBuffer(format.format(play.getpTime()));
        dateStr.replace(2,3,"月");
        dateStr.replace(5,6,"日 ");
        detailVo.setpTimeStr(dateStr.toString());

        Room room = roomMapper.selectById(play.getRoomId());
        // 可以加非空判断  但原则上  现有放映厅  才有排片信息
        detailVo.setSeat(room.getSituation());

        Film film= filmMapper.filmDetail(play.getfId());
        detailVo.setF_length(film.getF_length());
        detailVo.setImg_path(film.getImg_path());
        return detailVo;
    }

    @Override
    public Play selsetOne(String playID) {
        return playMapper.selectByPlayId(playID);
    }

    @Override
    public Map<String, String> seleceFilm() {
        List<Film> films = filmMapper.selectAll();
        Map<String, String> map = new HashMap<>();
        for (Film film : films) {
            map.put(film.getF_id(),film.getF_name());
        }
        return map;
    }

    @Override
    public Map<String, String> seleceRomm() {
        List<Room> rooms = roomMapper.selectAll();
        Map<String, String> map = new HashMap<>();
        for (Room room : rooms) {
            map.put(room.getRoomId(),room.getRoomName());
        }
        return map;
    }

    public int playInsert(Play play){
        if(filmMapper.filmDetail(play.getpId()) != null){
            return 0;
        }
        play.setfName(filmMapper.filmDetail(play.getfId()).getF_name());
        play.setRoomName(roomMapper.selectById(play.getRoomId()).getRoomName());
        return playMapper.insert(play);
    }

    public List<PlayJson> playAll(int page,int limit){
        Map<String,Integer> map = new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        List<Play> plays = playMapper.selectPage(map);
        List<PlayJson> jsons = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Play play : plays) {
            PlayJson json = new PlayJson();
            json.setP_id(play.getpId());
            json.setF_name(play.getfName());
            json.setLan_type(play.getLanType());
            json.setRoom_name(play.getRoomName());
            json.setPrice(play.getPrice().toString());

            json.setP_time(format.format(play.getpTime()));
            json.setSetup("<a href='' target='content' class='layui-btn layui-btn-sm layui-btn-danger'>修改</a>");
            jsons.add(json);
        }
        return jsons;
    }

    public int count(){
        return playMapper.selectAll().size();
    }

}
